package com.suedawn.bookmanagesystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.suedawn.bookmanagesystem.dto.BookDTO;
import com.suedawn.bookmanagesystem.dto.BookQueryDTO;
import com.suedawn.bookmanagesystem.entity.Book;
import com.suedawn.bookmanagesystem.service.BookService;
import com.suedawn.bookmanagesystem.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/book")
@Api(tags = "书籍接口")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/add")
    @ApiOperation("添加书籍")
    public R addBook(@RequestBody BookDTO vo){
        Book book = BeanUtil.copyProperties(vo, Book.class);
        boolean save = bookService.save(book);
        return R.ok("添加成功");
    }

    @ApiOperation("删除书籍")
    @DeleteMapping("/del")
    public R removeBook(Long id){
        boolean b = bookService.removeById(id);
        return b?R.ok("删除成功"):R.error();
    }

    @ApiOperation("获取书籍信息")
    @GetMapping("/list")
    public R listBook(BookQueryDTO dto){
        return bookService.listBook(dto);
    }

    @ApiOperation("更新书籍信息")
    @PutMapping("/update")
    public R updateBook(@RequestBody Book dto){
        boolean b = bookService.updateById(dto);
        return b?R.ok("修改成功"):R.error();
    }


}
